<style>
    .dn {
        display: none;
    }

    .attachments {
        display: block;
        float: left;
        overflow: hidden;
        text-align: center;
    }

    .option-item {
        overflow: hidden;
        margin-top: 5px;
        color: #1E9FFF;
        text-align: center;
    }

    .preview {
        cursor: pointer;
    }

    .file-a {
        font-size: 12px;
        word-break: break-all;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-top: 5px;
    }

    .cardU {
        background-image: url(../../assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: 36%;
        height: 150px;
        padding: 0;
    }

    .cardU img {
        width: 100%;
        height: 100%;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">编码：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">仓库：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="queryWarehouseId" name="queryWarehouseId" lay-filter="queryWarehouseId" lay-search lay-verify=""></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">库区：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="queryWarehouseAreaId" name="queryWarehouseAreaId" lay-filter="queryWarehouseAreaId" lay-search lay-verify=""></select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit>查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                        <!--    <button id="warehouseShelfFlatBtnAdd" class="layui-btn icon-btn color-reseda">添加平仓库位</button>
                            <button id="warehouseShelfBtnAdd" class="layui-btn icon-btn color-reseda">添加立体库位</button>-->
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseShelfTable" lay-filter="warehouseShelfTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseShelfTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>

    <a class="layui-btn layui-btn-xs" lay-event="shelfSpace">货位列表</a>

    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="shelfInventory">库存明细</a>
    -->
    <!--
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
</script>

<script type="text/html" id="warehouseShelfSpaceTableBar">
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="shelfSpaceInventory">存货明细</a>
    -->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseShelfForm">
    <form lay-filter="warehouseShelfForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名称" type="text" class="layui-input"
                           lay-verify="required|isOnlyName"
                           required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">编码</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入编码" type="text" class="layui-input"
                           lay-verify="required|isOnlySn"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="warehouseId" name="warehouseId" lay-filter="warehouseId" lay-search lay-verify="required"></select>
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">库区</label>
                <div class="layui-input-block">
                    <select id="warehouseAreaId" name="warehouseAreaId" lay-search lay-verify="required"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">货架列数</label>
                <div class="layui-input-block">
                    <input name="columnNum" placeholder="请输入货架列数" type="text" class="layui-input"
                           lay-verify="required|sortNumber" required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">货架层数</label>
                <div class="layui-input-block">
                    <input name="layerNum" placeholder="请输入货架层数" type="text" class="layui-input"
                           lay-verify="required|sortNumber" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">货架图片</label>
            <div class="layui-upload-drag cardU" id="pictureDiv">
                <!--<i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>-->
                <input type="hidden" name="picture" lay-verify="" data-name="仓库图片">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"
                           lay-verify="sortNumber"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" type="text" class="layui-input" style="height: 100px;"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseShelfFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="warehouseShelfFlatForm">
    <form lay-filter="warehouseShelfFlatForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="warehouseFlatId" name="warehouseId" lay-filter="warehouseFlatId" lay-search lay-verify="required"></select>
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">库区</label>
                <div class="layui-input-block">
                    <select id="warehouseAreaFlatId" name="warehouseAreaId" lay-search lay-verify="required"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">初始编码</label>
                <div class="layui-input-block">
                    <input name="firstCode" placeholder="请输入初始编码" type="text" class="layui-input"
                           lay-verify="required"
                           required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">库位数</label>
                <div class="layui-input-block">
                    <input name="spaceNum" placeholder="请输入库位数" type="text" class="layui-input"
                           lay-verify="required|isOnlySn"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"
                           lay-verify="sortNumber"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" type="text" class="layui-input" style="height: 100px;"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseShelfFlatFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="shelfSpace">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">编码：</label>
                            <div class="layui-input-inline mr0">
                                <input name="code" class="layui-input" type="text" placeholder="请输入编码"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="shelfSpaceformSearch" lay-submit><i
                                    class="layui-icon">&#xe615;</i>搜索
                            </button>
                            <!--
                            <button id="warehouseShelfSpaceBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            -->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="warehouseShelfSpaceTable" lay-filter="warehouseShelfSpaceTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="shelfInventory">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">


                <div class="layui-tab" lay-filter="shelfInventoryTab">
                    <ul class="layui-tab-title shelfInventoryTab">
                        <li class="layui-this" lay-id="mouldInventory">模具库存</li>
                        <li lay-id="componentInventory">组件库存</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <!-- 头部操作栏 -->
                            <div class="layui-form toolbar">
                                <div class="layui-form-item">
                                    <div class="layui-inline">

                                        <label class="layui-form-label">模具组号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="mouldGroupSn" class="layui-input" type="text"
                                                   placeholder="请输入模具组号"/>
                                        </div>

                                        <label class="layui-form-label ">货位</label>
                                        <div class="layui-input-inline mr0">
                                            <select id="shelfSpaceId" name="shelfSpaceId" lay-search lay-verify="">
                                                <option value=""> 请选择货位</option>
                                            </select>
                                        </div>

                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn" lay-filter="shelfMouldInventoryFormSearch"
                                                lay-submit><i class="layui-icon">&#xe615;</i>搜索
                                        </button>

                                    </div>
                                </div>
                            </div>
                            <!-- 表格 -->
                            <table class="layui-table" id="shelfMouldInventoryTable"
                                   lay-filter="shelfMouldInventoryTable"></table>
                        </div>
                        <div class="layui-tab-item">
                            <!-- 头部操作栏 -->
                            <div class="layui-form toolbar">
                                <div class="layui-form-item">
                                    <div class="layui-inline">


                                        <label class="layui-form-label ">组件编号</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="mouldComponentSn" class="layui-input" type="text"
                                                   placeholder="请输入组件编号"/>
                                        </div>

                                        <label class="layui-form-label ">货位</label>
                                        <div class="layui-input-inline mr0">
                                            <select id="shelfSpaceId2" name="shelfSpaceId" lay-search lay-verify="">
                                                <option value=""> 请选择货位</option>
                                            </select>
                                        </div>

                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn"
                                                lay-filter="shelfComponentInventoryFormSearch" lay-submit><i
                                                class="layui-icon">&#xe615;</i>搜索
                                        </button>

                                    </div>
                                </div>
                            </div>
                            <!-- 表格 -->
                            <table class="layui-table" id="shelfComponentInventoryTable"
                                   lay-filter="shelfComponentInventoryTable"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</script>


<script type="text/html" id="shelfSpaceInventory">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">


                <div class="layui-tab">
                    <ul class="layui-tab-title shelfSpaceInventoryTab">
                        <li class="layui-this" lay-id="mouldInventory2">模具库存</li>
                        <li lay-id="componentInventory2">组件库存</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <!-- 头部操作栏 -->
                            <div class="layui-form toolbar">
                                <div class="layui-form-item">
                                    <div class="layui-inline">

                                        <label class="layui-form-label">模具组号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="mouldGroupSn" class="layui-input" type="text"
                                                   placeholder="请输入模具组号"/>
                                        </div>

                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn"
                                                lay-filter="shelfSpaceMouldInventoryFormSearch" lay-submit><i
                                                class="layui-icon">&#xe615;</i>搜索
                                        </button>

                                    </div>
                                </div>
                            </div>
                            <!-- 表格 -->
                            <table class="layui-table" id="shelfSpaceMouldInventoryTable"
                                   lay-filter="shelfSpaceMouldInventoryTable"></table>
                        </div>
                        <div class="layui-tab-item">
                            <!-- 头部操作栏 -->
                            <div class="layui-form toolbar">
                                <div class="layui-form-item">
                                    <div class="layui-inline">


                                        <label class="layui-form-label ">组件编号</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="mouldComponentSn" class="layui-input" type="text"
                                                   placeholder="请输入组件编号"/>
                                        </div>

                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn"
                                                lay-filter="shelfSpaceComponentInventoryFormSearch" lay-submit><i
                                                class="layui-icon">&#xe615;</i>搜索
                                        </button>

                                    </div>
                                </div>
                            </div>
                            <!-- 表格 -->
                            <table class="layui-table" id="shelfSpaceComponentInventoryTable"
                                   lay-filter="shelfSpaceComponentInventoryTable"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</script>

<script type="text/html" id="mouldInventoryDetail">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">清单编号：</label>
                            <div class="layui-input-inline mr0">
                                <input name="sn" class="layui-input" type="text" placeholder="请输入清单编号"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="mouldInventoryDetailFormSubSearch"
                                    lay-submit><i class="layui-icon">&#xe615;</i>搜索
                            </button>
                            <!--<button id="mouldBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button id="mouldBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="mouldInventoryDetailTable"
                       lay-filter="mouldInventoryDetailTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="mouldGroupShelfSpaceInventoryTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">存货清单</a>
</script>

<!-- js部分 -->
<script>

    var $, config;

    function stopBubbling(e) {
        e = window.event || e;
        if (e.stopPropagation) {
            e.stopPropagation();      //阻止事件 冒泡传播
        } else {
            e.cancelBubble = true;   //ie兼容
        }
    }

    //下载文件
    function downloadFile(realName, url) {
        stopBubbling(this);

        var $downForm = $("<form method='get'></form>");
        $downForm.attr("action", config.base_server + "api-file/downloadFile");
        var $input = $("<input type='hidden'>");
        $input.attr("name", "url");
        $input.val(url);
        $downForm.append($input);
        var $input1 = $("<input type='hidden'>");
        $input1.attr("name", "filename");
        $input1.val(realName);
        $downForm.append($input1);
        var $input2 = $("<input type='hidden'>");
        $input2.attr("name", "access_token");
        $input2.val(config.getToken().access_token);
        $downForm.append($input2);
        $(document.body).append($downForm);
        //提交表单，实现下载
        $downForm.submit();
    }

    //预览
    function previewImg(index) {
        stopBubbling(this);

        var obj = $(index).parent().prev().prev();
        var img = new Image();
        img.src = obj.attr("src");
        var height = img.height; //获取图片高度
        var width = img.width; //获取图片宽度
        if (width > 1000) {
            height = 1000 / width * height;
            width = 1000;
            if (height > 800) {
                width = 800 / height * width;
                height = 800;
            }
        }
        if (height > 800) {
            width = 800 / height * width;
            height = 800;
        }

        var imgHtml = "<img src='" + obj.attr("src") + "' style='width: " + width + "px; height: " + height + "px;' />";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [width + 'px', (height + 50) + 'px'],
            shadeClose: true,//点击外围关闭弹窗
            scrollbar: false,//不现实滚动条
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }

    layui.use(['layer', 'form', 'table', 'admin', 'laytpl', 'upload', 'element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var element = layui.element;

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseShelfTable',
            url: config.base + 'enterprisecab/warehouse/admin/warehouseShelf/list',
            page: true,
            where:{
                sort:"sort_number",
                order:"asc"
            },
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                , {field: 'name', align: 'center', title: '名称'}
                , {field: 'code', align: 'center', title: '编码'}
                , {field: 'warehouseName', align: 'center', title: '仓库'}
                , {field: 'warehouseAreaName', align: 'center', title: '仓库库区'}
                , {field: 'statusDesc', align: 'center', title: '状态'}
                , {field: 'sortNumber', align: 'center', title: '序号'}
                , {field: 'description', align: 'center', title: '备注'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 180}
                , {align: 'center', toolbar: '#warehouseShelfTableBar', title: '操作', width: 200}
            ]]
        });

        //仓库列表
        admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "queryWarehouseId", null, "仓库", null, null);
        //仓库选择事件
        form.on('select(queryWarehouseId)', function (data) {
            $("#queryWarehouseAreaId").empty();
            //仓库库区列表
            admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.value}, "queryWarehouseAreaId", null, "库区", null, null);
        });


        // 工具条点击事件
        table.on('tool(warehouseShelfTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            } else if (obj.event == 'shelfSpace') {
                showShelfSpace(data);
            } else if (obj.event == 'shelfInventory') {
                showInventoryDetail(data);
            }
        });

        //监听排序
        table.on('sort(warehouseShelfTable)', function (obj) {
            table.reload('warehouseShelfTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        form.verify({
            isOnlySn: function (value) {
                var isOnly = true;
                var idVal = $("form input[name='id']").val();
                admin.req('enterprisecab/warehouse/admin/warehouseShelf/isOnly', {
                    code: value,
                    id: idVal
                }, function (res) {
                    if (res.code == 200) {
                        isOnly = res.data;
                    }
                }, 'GET', false);
                if (!isOnly) {
                    return "仓库货架编码必须唯一"
                }
            },
            isOnlyName: function (value) {
                var isOnly = true;
                var idVal = $("form input[name='id']").val();
                var nameVal = $("form input[name='name']").val();
                admin.req('enterprisecab/warehouse/admin/warehouseShelf/isOnly', {
                    name: nameVal,
                    id: idVal
                }, function (res) {
                    if (res.code == 200) {
                        isOnly = res.data;
                    }
                }, 'GET', false);
                if (!isOnly) {
                    return "仓库货架名称必须唯一"
                }
            }
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#warehouseShelfBtnAdd').click(function () {
            showEditModel();
        });

        // 添加按钮点击事件
        $('#warehouseShelfFlatBtnAdd').click(function () {
            showFlatEditModel();
        });

        function showMouldInventoryDetail(data) {
            admin.open({
                type: 1,
                area: ['90%', '80%'],
                offset: '120px',
                title: '模具清单列表',
                content: $('#mouldInventoryDetail').html(),
                success: function () {
                    // 渲染表格
                    var mouldTb = table.render({
                        elem: '#mouldInventoryDetailTable',
                        url: config.base_server + 'mes/mould/admin/mouldBatchDetail?shelfSpaceId=' + data.shelfSpaceId + '&mouldGroupId=' + data.mouldGroupId,
                        where: {access_token: config.getToken().access_token},
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            , {field: 'mouldBatchSn', align: 'center', title: '模具批次编号'}
                            , {field: 'sn', align: 'center', title: '模具清单编号'}
                            , {field: 'mouldGroupSn', align: 'center', title: '模具组编号'}
                            , {field: 'mouldTypeName', align: 'center', title: '模具机型'}
                            , {field: "surplusTime", align: 'center', title: '剩余时长'}
                            , {field: "surplusQuantity", align: 'center', title: '剩余产量'}
                            , {field: "supplierName", align: 'center', title: '供应商'}
                            , {field: 'stateDesc', align: 'center', title: '模具状态'}
                            , {field: 'inventoryStateDesc', align: 'center', title: '库存状态'}
                            , {field: 'description', align: 'center', title: '备注'}
                            , {field: 'creatorName', align: 'center', title: '创建人'}
                            , {field: 'createTime', align: 'center', title: '创建时间'}
                            // , {align: 'center', toolbar: '#mouldInventoryDetailTableBar', title: '操作', width: 350}
                        ]]
                    });
                    // 工具条点击事件
                    table.on('tool(mouldInventoryDetailTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') { //修改
                            data.eventType = 'detail'
                        } else if (obj.event == 'del') {
                            doDelete(obj);
                        } else if (obj.event == 'batchDetailLife') {

                        }
                    });

                    //监听排序
                    table.on('sort(mouldInventoryDetailTable)', function (obj) {
                        table.reload('mouldInventoryDetailTable', {
                            initSort: obj
                            , where: {
                                sort: obj.field
                                , order: obj.type
                            }
                        });
                    });

                    // 搜索
                    form.on('submit(mouldInventoryDetailFormSubSearch)', function (data) {
                        mouldTb.reload({where: data.field, page: {curr: 1}}, 'data');
                    });


                }
            })
        };

        var getMouldInventoryDetail = function (data) {
            // 渲染表格
            var batchInventoryInOutTb = table.render({
                elem: '#shelfMouldInventoryTable',
                url: config.base_server + 'mes/mould/admin/mouldGroupShelfSpaceInventory?warehouseShelfId=' + data.id,
                where: {access_token: config.getToken().access_token},
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    , {field: 'mouldGroupName', align: 'center', title: '模具组名称'}
                    , {field: 'mouldGroupSn', align: 'center', title: '模具组编号'}
                    , {field: 'mouldTypeName', align: 'center', title: '机型'}
                    , {field: 'quantity', align: 'center', title: '数量'}
                    , {field: 'warehouseName', align: 'center', title: '仓库'}
                    , {field: 'warehouseAreaName', align: 'center', title: '库区'}
                    , {field: 'warehouseShelfName', align: 'center', title: '货架'}
                    , {field: 'shelfSpaceSn', align: 'center', title: '货位'}
                    , {field: 'creatorName', align: 'center', title: '创建人'}
                    , {field: 'createTime', align: 'center', title: '创建时间'}

                    , {align: 'center', toolbar: '#mouldGroupShelfSpaceInventoryTableBar', title: '操作', width: 200}
                ]]
            });

            admin.req('enterprisecab/warehouse/admin/warehouseShelfSpace', {shelfId: data.id}, function (res) {

                if (200 == res.code) {
                    var optionHtml = '<option value=""> 请选择货架货位</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        optionHtml += '<option value="' + res.data[k].id + '">' + res.data[k].sn + '</option>';
                    }
                    $("select[name='shelfSpaceId']").html(optionHtml);
                    form.render('select');
                } else {
                    layer.msg('获取仓库失败', {icon: 2, time: 1000});
                }
            }, 'GET');

            // 工具条点击事件
            table.on('tool(shelfMouldInventoryTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                } else if (obj.event == 'detail') {
                    showMouldInventoryDetail(data);
                }
            });

            //监听排序
            table.on('sort(shelfMouldInventoryTable)', function (obj) {
                table.reload('shelfMouldInventoryTable', {
                    initSort: obj
                    , where: {
                        sort: obj.field
                        , order: obj.type
                    }
                });
            });

            // 搜索
            form.on('submit(shelfMouldInventoryFormSearch)', function (data) {
                batchInventoryInOutTb.reload({where: data.field, page: {curr: 1}}, 'data');
            });


        }

        var getComponentInventoryDetail = function (data) {
            //component
            var componentInventoryInOutTb = table.render({
                elem: '#shelfComponentInventoryTable',
                url: config.base_server + 'mes/mould/admin/mouldComponentShelfSpaceInventory?warehouseShelfId=' + data.id,
                where: {access_token: config.getToken().access_token},
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    // ,{field: 'sn', align:'center', title: '单据号'}
                    , {field: 'mouldComponentSn', align: 'center', title: '组件编号'}
                    , {field: 'mouldComponentName', align: 'center', title: '组件名称'}
                    , {field: 'componentTypeName', align: 'center', title: '组件类型'}
                    , {field: 'quantity', align: 'center', title: '数量'}
                    , {field: 'warehouseName', align: 'center', title: '仓库'}
                    , {field: 'warehouseAreaName', align: 'center', title: '库区'}
                    , {field: 'warehouseShelfName', align: 'center', title: '货架'}
                    , {field: 'shelfSpaceSn', align: 'center', title: '货位'}
                    , {field: 'creatorName', align: 'center', title: '创建人'}
                    , {field: 'createTime', align: 'center', title: '创建时间'}


                    // ,{align: 'center', toolbar: '#shelfComponentInventoryTableBar', title: '操作', width: 200}
                ]]
            });

            admin.req('enterprisecab/warehouse/admin/warehouseShelfSpace', {shelfId: data.id}, function (res) {

                if (200 == res.code) {
                    var optionHtml = '<option value=""> 请选择货架货位</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        optionHtml += '<option value="' + res.data[k].id + '">' + res.data[k].sn + '</option>';
                    }
                    $("select[name='shelfSpaceId']").html(optionHtml);
                    form.render('select');
                } else {
                    layer.msg('获取仓库失败', {icon: 2, time: 1000});
                }
            }, 'GET');

            // 工具条点击事件
            table.on('tool(shelfComponentInventoryTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }
            });

            //监听排序
            table.on('sort(shelfComponentInventoryTable)', function (obj) {
                table.reload('shelfComponentInventoryTable', {
                    initSort: obj
                    , where: {
                        sort: obj.field
                        , order: obj.type
                    }
                });
            });

            // 搜索
            form.on('submit(shelfComponentInventoryFormSearch)', function (data) {
                componentInventoryInOutTb.reload({where: data.field, page: {curr: 1}}, 'data');
            });


        }

        function showInventoryDetail(data) {
            admin.open({
                type: 1,
                area: ['90%', '80%'],
                offset: '120px',
                title: '存货明细',
                content: $('#shelfInventory').html(),
                success: function () {

                    getMouldInventoryDetail(data);

                    $('.shelfInventoryTab li').on('click', function () {
                        var othis = $(this);
                        var layId = othis.attr("lay-id")

                        element.tabChange('shelfInventoryTab', layId);
                        if (layId == 'mouldInventory') {
                            getMouldInventoryDetail(data);
                        } else if (layId == 'componentInventory') {
                            getComponentInventoryDetail(data);
                        }
                    });


                }
            });
        }

        function showShelfSpace(data) {
            admin.open({
                type: 1,
                area: '70%',
                offset: '120px',
                title: '货位列表',
                content: $('#shelfSpace').html(),
                success: function () {
                    var insTb = table.render({
                        elem: '#warehouseShelfSpaceTable',
                        url: config.base + 'enterprisecab/warehouse/admin/warehouseShelfSpace/list?shelfId=' + data.id+"&status=1",
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers'}
                            , {field: 'code', align: 'center', title: '货位编码'}
                            , {field: 'warehouseName', align: 'center', title: '仓库'}
                            , {field: 'warehouseAreaName', align: 'center', title: '仓库库区'}
                            , {field: 'shelfName', align: 'center', title: '货架'}
                            , {field: 'description', align: 'center', title: '备注'}
                            , {field: 'createTime', align: 'center', title: '创建时间'}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(warehouseShelfSpaceTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') { //修改
                            showEditModel(data);
                        } else if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        } else if (obj.event == 'shelfSpaceInventory') {
                            showInventoryDetail(data);
                        }
                    });

                    //监听排序
                    table.on('sort(warehouseShelfSpaceTable)', function (obj) {
                        table.reload('warehouseShelfSpaceTable', {
                            initSort: obj,
                            where: {
                                sort: obj.field,
                                order: obj.type
                            }
                        });
                    });

                    // 搜索
                    form.on('submit(shelfSpaceformSearch)', function (data) {
                        insTb.reload({where: data.field, page: {curr: 1}}, 'data');
                    });

                    var getMouldInventoryDetail = function (data) {
                        // 渲染表格
                        var batchInventoryInOutTb = table.render({
                            elem: '#shelfSpaceMouldInventoryTable',
                            url: config.base + 'mes/mould/admin/mouldGroupShelfSpaceInventory?shelfSpaceId=' + data.id,
                            page: true,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'checkbox'}
                                , {field: 'mouldGroupName', align: 'center', title: '模具组名称'}
                                , {field: 'mouldGroupSn', align: 'center', title: '模具组编号'}
                                , {field: 'mouldTypeName', align: 'center', title: '机型'}
                                , {field: 'quantity', align: 'center', title: '数量'}
                                , {field: 'warehouseName', align: 'center', title: '仓库'}
                                , {field: 'warehouseAreaName', align: 'center', title: '库区'}
                                , {field: 'warehouseShelfName', align: 'center', title: '货架'}
                                , {field: 'shelfSpaceSn', align: 'center', title: '货位'}
                                , {field: 'creatorName', align: 'center', title: '创建人'}
                                , {field: 'createTime', align: 'center', title: '创建时间'}
                                , {
                                    align: 'center',
                                    toolbar: '#mouldGroupShelfSpaceInventoryTableBar',
                                    title: '操作',
                                    width: 200
                                }
                            ]]

                        });


                        // 工具条点击事件
                        table.on('tool(shelfSpaceMouldInventoryTable)', function (obj) {
                            var data = obj.data;
                            if (obj.event === 'edit') { //修改

                            } else if (obj.event === 'del') { // 删除

                            } else if (obj.event == 'detail') {
                                showMouldInventoryDetail(data);
                            }
                        });

                        //监听排序
                        table.on('sort(shelfSpaceMouldInventoryTable)', function (obj) {
                            table.reload('shelfSpaceMouldInventoryTable', {
                                initSort: obj,
                                where: {
                                    sort: obj.field,
                                    order: obj.type
                                }
                            });
                        });

                        // 搜索
                        form.on('submit(shelfSpaceMouldInventoryFormSearch)', function (data) {
                            batchInventoryInOutTb.reload({where: data.field, page: {curr: 1}}, 'data');
                        });
                    }

                    var getComponentInventoryDetail = function (data) {
                        //component
                        var componentInventoryInOutTb = table.render({
                            elem: '#shelfSpaceComponentInventoryTable',
                            url: config.base_server + 'mes/mould/admin/mouldComponentShelfSpaceInventory?shelfSpaceId=' + data.id,
                            where: {access_token: config.getToken().access_token},
                            page: true,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'checkbox'}
                                // ,{field: 'sn', align:'center', title: '单据号'}
                                , {field: 'mouldComponentSn', align: 'center', title: '组件编号'}
                                , {field: 'mouldComponentName', align: 'center', title: '组件名称'}
                                , {field: 'componentTypeName', align: 'center', title: '组件类型'}
                                , {field: 'quantity', align: 'center', title: '数量'}
                                , {field: 'warehouseName', align: 'center', title: '仓库'}
                                , {field: 'warehouseAreaName', align: 'center', title: '库区'}
                                , {field: 'warehouseShelfName', align: 'center', title: '货架'}
                                , {field: 'shelfSpaceSn', align: 'center', title: '货位'}
                                , {field: 'creatorName', align: 'center', title: '创建人'}
                                , {field: 'createTime', align: 'center', title: '创建时间'}
                            ]]
                        });


                        // 工具条点击事件
                        table.on('tool(shelfSpaceComponentInventoryTable)', function (obj) {
                            var data = obj.data;
                            if (obj.event === 'edit') { //修改
                                showEditModel(data);
                            } else if (obj.event === 'del') { // 删除
                                doDelete(obj);
                            }
                        });

                        //监听排序
                        table.on('sort(shelfSpaceComponentInventoryTable)', function (obj) {
                            table.reload('shelfSpaceComponentInventoryTable', {
                                initSort: obj,
                                where: {
                                    sort: obj.field,
                                    order: obj.type
                                }
                            });
                        });

                        // 搜索
                        form.on('submit(shelfSpaceComponentInventoryFormSearch)', function (data) {
                            componentInventoryInOutTb.reload({where: data.field, page: {curr: 1}}, 'data');
                        });
                    }

                    function showInventoryDetail(data) {
                        admin.open({
                            type: 1,
                            area: ['90%', '80%'],
                            offset: '120px',
                            title: '存货明细',
                            content: $('#shelfSpaceInventory').html(),
                            success: function () {
                                getMouldInventoryDetail(data);

                                $('.shelfSpaceInventoryTab li').on('click', function () {
                                    var othis = $(this);
                                    var layId = othis.attr("lay-id")

                                    element.tabChange('shelfSpaceInventoryTab', layId);
                                    if (layId == 'mouldInventory2') {
                                        getMouldInventoryDetail(data);
                                    } else if (layId == 'componentInventory2') {
                                        getComponentInventoryDetail(data);
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }

        function showFlatEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: ['120px', '550px'],
                title: data ? '修改仓库货架' : '添加仓库货架',
                content: $('#warehouseShelfFlatForm').html(),
                success: function () {
                    //仓库列表
                    admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "warehouseFlatId", data ? data.warehouseId : null, "仓库", null, null);
                    //仓库选择事件
                    form.on('select(warehouseFlatId)', function (data) {
                        $("#warehouseAreaFlatId").empty();
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.value}, "warehouseAreaFlatId", null, "库区", null, null);
                    });
                    // 表单提交事件
                    form.on('submit(warehouseShelfFlatFormSubmit)', function (d) {
                        layer.load(2);
                        var url = 'enterprisecab/warehouse/admin/warehouseShelfSpace/addFlat';
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseShelfTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }
        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: '120px',
                title: data ? '修改仓库货架' : '添加仓库货架',
                content: $('#warehouseShelfForm').html(),
                success: function () {
                    //仓库列表
                    admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "warehouseId", data ? data.warehouseId : null, "仓库", null, null);
                    //仓库选择事件
                    form.on('select(warehouseId)', function (data) {
                        $("#warehouseAreaId").empty();
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.value}, "warehouseAreaId", null, "库区", null, null);
                    });

                    //图片上传
                    upload.render({
                        elem: '#pictureDiv'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            layer.closeAll('loading');
                            layer.msg('上传成功');
                            var imgUrl = res.data.url;
                            //上传完毕回调
                            $("#pictureDiv").html('<img src="' + imgUrl + '" alt="上传成功后渲染"><input type="hidden" name="picture" value="' + imgUrl + '">');
                        }
                        , error: function (data) {
                            layer.closeAll('loading');
                        }
                    });

                    if (data != undefined) {
                        var picture = data.picture;
                        if (picture) {
                            $("#pictureDiv").html('<img src="' + picture + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="picture" value="' + picture + '">');
                        }
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.warehouseId}, "warehouseAreaId", data.warehouseAreaId, "库区", null, null);
                        //数据回显
                        form.val('warehouseShelfForm', data);
                    }

                    if (data) {
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.status == item.text) {
                                data.status = item.value;
                            }
                        });
                    }

                    if (data) {
                        var option = $("form select[name='warehouseFeatureType']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.warehouseFeatureType == item.text) {
                                data.warehouseFeatureType = item.value;
                            }
                        });
                    }

                    form.val('warehouseShelfForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseShelfFormSubmit)', function (d) {
                        debugger
                        layer.load(2);
                        var url = 'enterprisecab/warehouse/admin/warehouseShelf/add';
                        if (data) {
                            url = 'enterprisecab/warehouse/admin/warehouseShelf/update';
                        }
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseShelfTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/warehouse/admin/warehouseShelf/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#warehouseBtnExport').click(function () {
            var checkRows = table.checkStatus('warehouseShelfTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>